{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1988</td>\n",
       "      <td>36.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>85.0</td>\n",
       "      <td>7.131</td>\n",
       "      <td>0.00</td>\n",
       "      <td>40.1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1992</td>\n",
       "      <td>29.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>91.0</td>\n",
       "      <td>7.041</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1998</td>\n",
       "      <td>42.0</td>\n",
       "      <td>叔叔或者姑姑有一方患有糖尿病</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7.134</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号  性别  出生年份  体重指数          糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  \\\n",
       "0   1   0  1996  30.1             无记录  106.0    3.818     7.89       0.0   \n",
       "1   2   0  1988  27.5             无记录   84.0   -1.000     0.00      14.7   \n",
       "2   3   1  1988  36.5             无记录   85.0    7.131     0.00      40.1   \n",
       "3   4   1  1992  29.5             无记录   91.0    7.041     0.00       0.0   \n",
       "4   5   0  1998  42.0  叔叔或者姑姑有一方患有糖尿病    NaN    7.134     0.00       0.0   \n",
       "\n",
       "   患有糖尿病标识  \n",
       "0        0  \n",
       "1        0  \n",
       "2        1  \n",
       "3        0  \n",
       "4        1  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(r'./data/test_2022.csv', encoding = 'utf-8')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5070 entries, 0 to 5069\n",
      "Data columns (total 10 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   编号        5070 non-null   int64  \n",
      " 1   性别        5070 non-null   int64  \n",
      " 2   出生年份      5070 non-null   int64  \n",
      " 3   体重指数      5070 non-null   float64\n",
      " 4   糖尿病家族史    5070 non-null   object \n",
      " 5   舒张压       4823 non-null   float64\n",
      " 6   口服耐糖量测试   5070 non-null   float64\n",
      " 7   胰岛素释放实验   5070 non-null   float64\n",
      " 8   肱三头肌皮褶厚度  5070 non-null   float64\n",
      " 9   患有糖尿病标识   5070 non-null   int64  \n",
      "dtypes: float64(5), int64(4), object(1)\n",
      "memory usage: 830.0 KB\n"
     ]
    }
   ],
   "source": [
    "df.info(memory_usage=\"deep\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号  性别  出生年份  体重指数 糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  患有糖尿病标识\n",
       "0   1   0  1996  30.1    无记录  106.0    3.818     7.89       0.0        0\n",
       "1   2   0  1988  27.5    无记录   84.0   -1.000     0.00      14.7        0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_category = df.copy()\n",
    "df_category.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5070 entries, 0 to 5069\n",
      "Data columns (total 10 columns):\n",
      " #   Column    Non-Null Count  Dtype   \n",
      "---  ------    --------------  -----   \n",
      " 0   编号        5070 non-null   int64   \n",
      " 1   性别        5070 non-null   int64   \n",
      " 2   出生年份      5070 non-null   int64   \n",
      " 3   体重指数      5070 non-null   float64 \n",
      " 4   糖尿病家族史    5070 non-null   category\n",
      " 5   舒张压       4823 non-null   float64 \n",
      " 6   口服耐糖量测试   5070 non-null   float64 \n",
      " 7   胰岛素释放实验   5070 non-null   float64 \n",
      " 8   肱三头肌皮褶厚度  5070 non-null   float64 \n",
      " 9   患有糖尿病标识   5070 non-null   int64   \n",
      "dtypes: category(1), float64(5), int64(4)\n",
      "memory usage: 362.2 KB\n"
     ]
    }
   ],
   "source": [
    "df_category[\"糖尿病家族史\"] = df_category[\"糖尿病家族史\"].astype(\"category\")\n",
    "df_category.info(memory_usage=\"deep\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>编号</th>\n",
       "      <th>性别</th>\n",
       "      <th>出生年份</th>\n",
       "      <th>体重指数</th>\n",
       "      <th>糖尿病家族史</th>\n",
       "      <th>舒张压</th>\n",
       "      <th>口服耐糖量测试</th>\n",
       "      <th>胰岛素释放实验</th>\n",
       "      <th>肱三头肌皮褶厚度</th>\n",
       "      <th>患有糖尿病标识</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1996</td>\n",
       "      <td>30.1</td>\n",
       "      <td>无记录</td>\n",
       "      <td>106.0</td>\n",
       "      <td>3.818</td>\n",
       "      <td>7.89</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1988</td>\n",
       "      <td>27.5</td>\n",
       "      <td>无记录</td>\n",
       "      <td>84.0</td>\n",
       "      <td>-1.000</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14.7</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   编号  性别  出生年份  体重指数 糖尿病家族史    舒张压  口服耐糖量测试  胰岛素释放实验  肱三头肌皮褶厚度  患有糖尿病标识\n",
       "0   1   0  1996  30.1    无记录  106.0    3.818     7.89       0.0        0\n",
       "1   2   0  1988  27.5    无记录   84.0   -1.000     0.00      14.7        0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_category.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "338 µs ± 7.47 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit df_category.groupby(\"糖尿病家族史\").size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "802 µs ± 12.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit df.groupby(\"糖尿病家族史\").size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts.charts import Line,Bar,Pie\n",
    "from pyecharts import options as opts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pyecharts.charts.basic_charts.line.Line at 0x119b32ca6d8>"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#折线图\n",
    "line = Line()\n",
    "# x轴\n",
    "line.add_xaxis(df[\"编号\"].tolist()[:50])\n",
    "# y 轴\n",
    "line.add_yaxis(\"体重指数\", df[\"体重指数\"].tolist()[:50])\n",
    "line.add_yaxis(\"舒张压\", df[\"舒张压\"].tolist()[:50])\n",
    "# 图标配置\n",
    "line.set_global_opts(\n",
    "    title_opts=opts.TitleOpts(title=\"身体指数\"),\n",
    "    tooltip_opts=opts.TooltipOpts(trigger=\"axis\", axis_pointer_type=\"cross\")\n",
    ")\n",
    "\n",
    "line.render(\"results/test_line.html\")\n",
    "#若是notebook 直接使用  line.render_notebook()  可以在本地显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter_lab\\\\course_pandas\\\\results\\\\test_line2.html'"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#另一种写法\n",
    "line2 = (\n",
    "    Line()\n",
    "    .add_xaxis(df[\"编号\"].tolist()[:50])\n",
    "    .add_yaxis(\"体重指数\", df[\"体重指数\"].tolist()[:50])\n",
    "    .add_yaxis(\"舒张压\", df[\"舒张压\"].tolist()[:50])\n",
    "    .set_global_opts(\n",
    "    title_opts=opts.TitleOpts(title=\"身体指数\"),\n",
    "    tooltip_opts=opts.TooltipOpts(trigger=\"axis\", axis_pointer_type=\"cross\")\n",
    "    )\n",
    ")\n",
    "line2.render(\"results/test_line2.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['0', '1'], [3134, 1936])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 柱状图\n",
    "df_ill = df[\"患有糖尿病标识\"].value_counts()\n",
    "[str(x) for x in df_ill.index],df_ill.values.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 柱状图\n",
    "df_ill = df[\"患有糖尿病标识\"].value_counts()\n",
    "\n",
    "bar = (\n",
    "   Bar()\n",
    "    .add_xaxis([str(x) for x in df_ill.index])\n",
    "    .add_yaxis(\"是否患有糖尿病\" ,df_ill.values.tolist())\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"糖尿病患者统计\"))\n",
    ")\n",
    "\n",
    "bar.render(\"results/test_bar.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter_lab\\\\course_pandas\\\\results\\\\test_bar.html'"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "糖尿病家族史\n",
       "叔叔或姑姑有一方患有糖尿病     1084\n",
       "叔叔或者姑姑有一方患有糖尿病     214\n",
       "无记录               2897\n",
       "父母有一方患有糖尿病         875\n",
       "dtype: int64"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#饼形图\n",
    "df_status = df.groupby(\"糖尿病家族史\").size()\n",
    "df_status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('叔叔或姑姑有一方患有糖尿病', 1084),\n",
       " ('叔叔或者姑姑有一方患有糖尿病', 214),\n",
       " ('无记录', 2897),\n",
       " ('父母有一方患有糖尿病', 875)]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(df_status.index, df_status))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter_lab\\\\course_pandas\\\\results\\\\test_pie.html'"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#饼形图\n",
    "df_status = df.groupby(\"糖尿病家族史\").size()\n",
    "\n",
    "pie = (\n",
    "    Pie()\n",
    "    .add(\"比例\", list(zip(df_status.index, df_status)))\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(formatter=\"{b}:{c}\"))\n",
    ")\n",
    "pie.render(\"results/test_pie.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\jupyter_lab\\\\course_pandas\\\\results\\\\test_pie2.html'"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pie = (\n",
    "    Pie()\n",
    "    .add(\"比例\", list(zip(df_status.index, df_status)))\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(formatter=\"{b}:{d}%\"))\n",
    ")\n",
    "pie.render(\"results/test_pie2.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
